from ultralytics import YOLO

def simulate_image_recognition(image_path):
    model = YOLO('yolov8n.pt')
    results = model.predict('/home/ye/Desktop/person.jpg')
    labels = [
    {
        "label": result.names[int(box.cls)],
        "category": "物体",  # 可自定义分类逻辑
        "confidence": float(box.conf),
        "description": f"检测到{result.names[int(box.cls)]}"
    }
    for result in results
    for box in result.boxes
    ]
    return labels

result = simulate_image_recognition('/')
print(result)

# [{'label': 'bus', 'category': '物体', 'confidence': 0.8734486699104309, 'description': '检测到bus'}, 
#  {'label': 'person', 'category': '物体', 'confidence': 0.865691065788269, 'description': '检测到person'}, 
#  {'label': 'person', 'category': '物体', 'confidence': 0.8528355360031128, 'description': '检测到person'}, 
#  {'label': 'person', 'category': '物体', 'confidence': 0.8252246379852295, 'description': '检测到person'}, 
#  {'label': 'person', 'category': '物体', 'confidence': 0.2611125111579895, 'description': '检测到person'}, 
#  {'label': 'stop sign', 'category': '物体', 'confidence': 0.2550688683986664, 'description': '检测到stop sign'}]

# [{'label': 'person', 'category': '物体', 'confidence': 0.8843054175376892, 'description': '检测到person'}, 
#  {'label': 'horse', 'category': '物体', 'confidence': 0.793515145778656, 'description': '检测到horse'}]